package reader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;

import model.IDrawable;
import model.Point2D;

/**
 * TODO javadoc
 * @author Marcus Alt
 *
 */
public class GeometryReader {
	
	private File theFileToRead = null;
	private HashSet<IDrawable> theResultHashSet = null;
	
	/**
	 *Constructor 
	 * @param f the file to read
	 */
	public GeometryReader(File f) {
		this.theFileToRead = f;
		this.theResultHashSet = new HashSet<IDrawable>();
	}
	/**
	 * read a file with geometries
	 * @return the error logs as a string
	 * @throws IOException 
	 */
	public String readFile() throws IOException{
		String problemString = "";
		BufferedReader reader = new BufferedReader(new FileReader(this.theFileToRead));
		String line ="";
		int linenumber = 0;
		while((line = reader.readLine()) != null) {
			++linenumber;
			String [] splitStrings = line.split(";");
			if(splitStrings.length <3) {
				problemString += "Line " + linenumber + " contains less than three entries.";
			}else {
				problemString +=readPoint2D(splitStrings);
			}
			
		}
		reader.close();
		return problemString;
	}
	private String readPoint2D(String[] splitStrings) {
		String problemString = "";
		if(splitStrings[0].toLowerCase().equalsIgnoreCase("p")) {
			try {
			int x = Integer.valueOf(splitStrings[1]);
			int y = Integer.valueOf(splitStrings[2]);
			Point2D p = new Point2D(x, y);
			boolean add = this.theResultHashSet.add(p);
			if(!add) {
				problemString +="Der Punkt " +x+ "/" +y+" war bereits vorhanden";
			}
			
			}catch(NumberFormatException e){
				problemString +=e.getMessage();
			}
		}else {
			problemString += "Unknown geometry";
		}
		
		return problemString;
	}
	
	public HashSet<IDrawable> getTheResultHashSet() {
		return theResultHashSet;
	}
	

}
